/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pkgUtil.pkgSupportFile;

import java.io.File;
import java.io.FileNotFoundException;
import pkgUtil.pkgSupportFile.pkgFrameworkFile.IGrabable;
import pkgUtil.pkgSupportFile.pkgFrameworkFile.ManagerFile;

/**
 *
 * @author Administrador
 */
public abstract class PASFile {

    private File fileSelect;
    private IGrabable typeData;
    private IGrabable typeHeadData;
    private String errorValid;
    private boolean valid;
    private boolean cancelOperation;

    public PASFile(File fileSelect, IGrabable typeData,IGrabable typeHeadData) {
        cancelOperation=false;
        valid = true;
        errorValid = "PASFile OK";
        this.fileSelect = fileSelect;
        this.typeData = typeData;
        this.typeHeadData=typeHeadData;
    }

    /**
     * @return the fileSelect
     */
    public File getFileSelect() {
        return fileSelect;
    }

    /**
     * @param fileSelect the fileSelect to set
     */
    public void setFileSelect(File fileSelect) {
        this.fileSelect = fileSelect;
    }

    /**
     * @return the errorValid
     */
    public String getErrorValid() {
        return errorValid;
    }

    /**
     * @param errorValid the errorValid to set
     */
    public void setErrorValid(String errorValid) {
        this.errorValid = errorValid;
    }

    /**
     * @return the valid
     */
    public boolean isValid() {
        return valid;
    }

    /**
     * @param valid the valid to set
     */
    public void setValid(boolean valid) {
        this.valid = valid;
    }

    public boolean isFile() {

        if (fileSelect == null || fileSelect.isDirectory()) {
            return false;
        }

        return true;

    }

    public boolean valid() {
        String cadError = " ";
        boolean boolRes = true;
        if (getFileSelect() == null) {
            cadError = cadError + "- No existe File seleccionado\n";
            boolRes = false;
        }

        if (typeData == null) {
            cadError = cadError + "- No se estableció tipo de Dato para File";
            boolRes = false;
        }
        if (cadError.length() > 1) {
            this.setErrorValid(cadError.trim());
        } else {
            this.setErrorValid("PASFile OK");
        }

        this.setValid(boolRes);
        return valid;

    }

    public String getDescriptionState() {
        String cad;
        if (isValid()) {
            cad = "Archivo válido";
        } else {
            cad = "Archivo no valido";
        }
        cad = cad + " (" + getErrorValid() + ")";
        return cad;
    }

    protected ManagerFile getInstanceManagerFileForSave() throws ClassNotFoundException {
        ManagerFile mg = new ManagerFile(getFileSelect(), typeData,typeHeadData);
        return mg;
    }

    protected ManagerFile getInstanceManagerFileForRead() throws ClassNotFoundException, FileNotFoundException {
        ManagerFile mg = new ManagerFile(getFileSelect(), typeData,typeHeadData);
        mg.openForRead();
        return mg;
    }

    protected void rename(String newPathRename, ManagerFile mgFile) throws ClassNotFoundException {
        ManagerFile mgPasRename = new ManagerFile(newPathRename, typeData,typeHeadData);
        mgFile.rename(mgPasRename);
    }

    public abstract void save() throws SavePASFileException;

    public abstract void read() throws ReadPASFileException;

    /**
     * @return the cancelOperation
     */
    public boolean isCancelOperation() {
        return cancelOperation;
    }

    /**
     * @param cancelOperation the cancelOperation to set
     */
    public void cancelOperation() {
        this.cancelOperation = true;
    }
    
}
